#include "Includes.h"

Bitmap::Bitmap(char* imgsrc, int x, int y, int w, int h)
{
	imgpos.x = x;
	imgpos.y = y;
	imgpos.h = h-y;
	imgpos.w = w-x;
	if((surface = load_img(imgsrc, IMG_PNG)) == NULL)
	{
		printf("Image failed to load");
	}
	Uint16 colorkey = SDL_MapRGB( surface->format, 0xf8, 0xfc, 0xf8 );
	//printf("%i", colorkey);
	SDL_SetColorKey(surface, SDL_SRCCOLORKEY , colorkey);
}

Bitmap::~Bitmap()
{
	SDL_FreeSurface(surface);
}

void Bitmap::set_position(int o)
{
	imgpos.x = o;
}

SDL_Rect Bitmap::get_position()
{
	return imgpos;
}

void Bitmap::blit(SDL_Surface *screen)
{
	SDL_BlitSurface(surface,&imgpos,screen,NULL);
}

SDL_Surface *Bitmap::get_surface()
{
	return surface;
}

void Bitmap::change(SDL_Surface *changeSurface)
{
	surface = changeSurface;
}

SDL_Surface *Bitmap::load_img(char *file, int type)
{
	SDL_RWops *rwop;
	SDL_Surface *image;
	
	rwop = SDL_RWFromFile(file, "rv");
	
	if(type == IMG_PNG)
		image = IMG_LoadPNG_RW(rwop);
	else if(type == IMG_GIF)
		image = IMG_LoadGIF_RW(rwop);
	
	if (image == NULL) {
		printf("Unable to load File: %s\n", SDL_GetError());
		return NULL;
	}
	return image;
}